% =================================
% All whinny data
%
% MFCC+energy+delta+delta&delta
% GMM with 3 mixtures
% =================================

function data_prepare_allcalltype(feature)

file_dir=dir('allcalltype');
for i=1:length(file_dir)-2
    calltype{i}=file_dir(i+2).name;
end

% Construct grammer file

fid=fopen('gram','w');
fprintf(fid,'$calltype=');
count=0;
for i=1:length(calltype)
    count=count+1;
    if(count==length(calltype))
        fprintf(fid,['\t' calltype{length(calltype)} ';\n']);
    else
        fprintf(fid,['\t' calltype{i} '|\n']);
    end
end
fprintf(fid,'\n(SENT-END $calltype SENT-END)\n');
fclose(fid)

% ============= Script file ==========
fid=fopen('codewords.scp','w');
rmdir('Data_exp/MFC','s');
mkdir('Data_exp/MFC');
for k=1:length(calltype)
    files_eachtype=dir(['allcalltype\' calltype{k} ]);
    mkdir(['Data_exp/MFC/' calltype{k}]);
    for j=1:length(files_eachtype)-2
        fprintf(fid,['allcalltype/' calltype{k} '/' files_eachtype(j+2).name '\t']);
        fprintf(fid,['Data_exp/MFC/' calltype{k} '/' calltype{k} '_' num2str(j) '.mfc \n']);
    end
end
fclose(fid);

% ===== Label files =========

fid=fopen('label.mlf','w');
fprintf(fid,'#!MLF!#\n');

for k=1:length(calltype)
    files_eachtype=dir(['allcalltype\' calltype{k} ]);
    for j=1:length(files_eachtype)-2
        fprintf(fid,['"*/' calltype{k} '_' num2str(j) '.lab" \n']);
        fprintf(fid,'sil \n')
        fprintf(fid,[calltype{k} '\n']);
        fprintf(fid,'sil \n');
        fprintf(fid,'.\n');
    end
end
fclose(fid);

% =========== Model List ================

% Define phone models

fid=fopen('model.list','w');
for i=1:length(calltype)
    fprintf(fid,[calltype{i} '\n']);
end
fprintf(fid, 'sil \n');
fclose(fid);
% ===== Construct Dictionary file ===============
fid = fopen('dict','w');
fprintf(fid, 'SENT-START []   sil\n');
fprintf(fid, 'SENT-END []   sil\n');
for i =1:length(calltype)
    fprintf(fid,[calltype{i} '\t']);
    fprintf(fid,[calltype{i} '\n']);
end
fclose(fid);
% =========== config ========
fid=fopen('config','w');
fprintf(fid, [' TARGETKIND = ' feature '\n']);
fprintf(fid,' SOURCEKIND = WAVEFORM \n');
fprintf(fid, 'SOURCEFORMAT = WAV\n');
fprintf(fid, 'TARGETRATE 	= 100000.0\n');
fprintf(fid, 'SAVECOMPRESSED 	= F\n');
fprintf(fid, 'SAVEWITHCRC 	= F\n');
fprintf(fid, 'WINDOWSIZE 	= 250000.0\n');
fprintf(fid, 'USEHAMMING 	= T\n');
fprintf(fid, 'ENORMALISE	= T\n');
fprintf(fid, 'ZMEANSOURCE	= F\n');
fprintf(fid, 'PREEMCOEF 	= 0.97\n');
fprintf(fid, 'NUMCHANS 	= 60\n');
fprintf(fid, 'CEPLIFTER 	= 22\n');
fprintf(fid, 'NUMCEPS 	= 12\n');
fprintf(fid, 'MAXFREQ 	= 10000\n');
fprintf(fid, 'MINFREQ 	= 10\n');
fclose(fid);

% % ====== Data prepare  bathfile============

fid=fopen('dataprep.bat','w');
fprintf(fid, 'HParse gram wdnet \n');
fprintf(fid, 'HCopy -C config -S codewords.scp \n');
status=fclose(fid);
dos('dataprep.bat');

%  ============Script file for training and testing===========
num_cross_val=5;
for I=1:num_cross_val
    fid_train=fopen(['train' num2str(I) '.scp'],'w');
    fid_test=fopen(['test' num2str(I) '.scp'],'w');
    for k=1:length(calltype)
        av_files=dir(['Data_exp/MFC/' calltype{k}]);
        test_size=floor(length(av_files)/num_cross_val);
        if I==1
            for j=I*test_size+1:length(av_files)-2
                fprintf(fid_train,['Data_exp/MFC/' calltype{k} '/' av_files(j+2).name '\n']);
            end
            for j=1:I*test_size
                fprintf(fid_test,['Data_exp/MFC/' calltype{k} '/' av_files(j+2).name '\n']);
            end
        else if I==num_cross_val
                for j=1:(I-1)*test_size
                    fprintf(fid_train,['Data_exp/MFC/' calltype{k} '/' av_files(j+2).name '\n']);
                end
                for j=(I-1)*test_size+1:length(av_files)-2
                    fprintf(fid_test,['Data_exp/MFC/' calltype{k} '/' av_files(j+2).name '\n']);
                end
            else
                for j=1:(I-1)*test_size
                    fprintf(fid_train,['Data_exp/MFC/' calltype{k} '/' av_files(j+2).name '\n']);
                end
                for j=I*test_size+1:length(av_files)-2
                    fprintf(fid_train,['Data_exp/MFC/' calltype{k} '/' av_files(j+2).name '\n']);
                end
                for j=(I-1)*test_size+1:I*test_size
                    fprintf(fid_test,['Data_exp/MFC/' calltype{k} '/' av_files(j+2).name '\n']);
                end
            end
        end
    end
    status=fclose(fid_train);
    status=fclose(fid_test);
end


